package cn.hg.solon.youcan.system.provider;

import java.util.List;
import java.util.Map;

import org.dromara.hutool.core.bean.BeanUtil;
import org.dromara.hutool.core.bean.copier.CopyOptions;
import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.text.CharSequenceUtil;
import org.dromara.hutool.core.text.StrValidator;
import org.dromara.hutool.db.PageResult;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;

import com.easy.query.api4j.client.EasyQuery;
import com.easy.query.api4j.select.Queryable;
import com.easy.query.core.api.pagination.EasyPageResult;
import com.easy.query.solon.annotation.Db;

import cn.hg.solon.youcan.common.constant.WebConstants;
import cn.hg.solon.youcan.system.entity.EqNotice;
import cn.hg.solon.youcan.system.entity.Notice;
import cn.hg.solon.youcan.system.service.NoticeService;

/**
 * @author 胡高
 */
@Component
public class EqNoticeProvider implements NoticeService {

    @Db("db1")
    private EasyQuery easyQuery;

    private Queryable<EqNotice> buildQuery(Map<String, Object> paraMap) {
        String word = (String)paraMap.get("word");
        String type = (String)paraMap.get("type");
        String status = (String)paraMap.get("status");
        String sortField = (String)paraMap.get("sortField");
        String sortType = (String)paraMap.get("sortType");

        boolean customOrder = StrValidator.isNotBlank(sortField) && StrValidator.isNotBlank(sortType);

        // FROM sys_notice AS t
        return this.easyQuery.queryable(EqNotice.class)
            // WHERE t.`type` = ${type} AND t.`status` = ${status}
            //      AND (t.`title` LIKE '%${word}%' OR t.`content` LIKE '%${word}%' OR t.`remark` LIKE '%${word}%')
            .where(w -> w.eq(StrValidator.isNotBlank(type), EqNotice::getType, type)
                .eq(StrValidator.isNotBlank(status), EqNotice::getStatus, status)
                .and(StrValidator.isNotBlank(word),
                    a -> a.like(EqNotice::getTitle, word)
                    .or().like(EqNotice::getContent, word)
                    .or().like(EqNotice::getRemark, word)))
            // ORDER BY ${sortField} DESC
            .orderBy(customOrder, ob -> ob.sqlNativeSegment(CharSequenceUtil.toUnderlineCase(sortField)),
                !CharSequenceUtil.equalsIgnoreCase(sortType, WebConstants.ORDER_DIRECTION_DESC));
    }

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.system.service.NoticeService#delete(java.util.List)
     */
    @Tran
    @Override
    public boolean delete(List<Integer> idList) {
        return this.easyQuery.deletable(EqNotice.class)
            .where(w -> w.in(EqNotice::getId, idList))
            .executeRows() > 0;
    }

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.system.service.NoticeService#get(java.lang.Integer)
     */
    @Override
    public Notice get(Integer id) {
        return this.easyQuery.queryable(EqNotice.class).whereById(id).firstOrNull();
    }

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.system.service.NoticeService#insert(cn.hg.solon.youcan.system.entity.Notice)
     */
    @Tran
    @Override
    public boolean insert(Notice bean) {
        EqNotice cloneBean = BeanUtil.copyProperties(bean, EqNotice.class);

        return this.easyQuery.insertable(cloneBean).executeRows() > 0;
    }

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.system.service.NoticeService#pageBy(int, int, java.util.Map)
     */
    @Override
    public PageResult<? extends Notice> pageBy(int pageNumber, int pageSize, Map<String, Object> paraMap) {
        EasyPageResult<EqNotice> pageList = this.buildQuery(paraMap).toPageResult(Convert.toLong(pageNumber), pageSize);

        PageResult<EqNotice> result = new PageResult<>();
        result.addAll(pageList.getData());
        result.setTotal(Convert.toInt(pageList.getTotal()));

        return result;
    }

    /* (non-Javadoc)
     * @see cn.hg.solon.youcan.system.service.NoticeService#update(cn.hg.solon.youcan.system.entity.Notice)
     */
    @Tran
    @Override
    public boolean update(Notice bean) {
        EqNotice cloneBean = (EqNotice)this.get(bean.getId());

        BeanUtil.copyProperties(bean, cloneBean, CopyOptions.of().setIgnoreNullValue(true));

        return this.easyQuery.updatable(cloneBean).executeRows() > 0;
    }

}
